/**********************************************
 * 
 * File Name				:	BusibuildingSecurityInterceptor.java
 * 
 * Last Changed Date		: 	$Date: 2009-08-01 11:12:53 +0800 (星期六, 01 八月 2009) $
 * 
 * Last Changed Revision	:	$Revision: 86 $
 * 
 * Last Changed Author		:	$Author: cfanboy $
 * 
 * File Header URL			:	$HeadURL: svn://www.svnhost.cn/Busi-Building/code/branch/src/com/busibuilding/common/interceptor/BusibuildingSecurityInterceptor.java $
 * 
 * ********************************************
 */

package com.busibuilding.common.interceptor;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.busibuilding.administrate.security.action.ProcessAdminLogonAction;
import com.busibuilding.common.Constants;
import com.busibuilding.common.action.CommonActionImpl;
import com.busibuilding.utils.CookieUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.interceptor.PreResultListener;

/**
 * Class Name: BusibuildingSecurityInterceptor
 * Description:  intercept the action to check the user whether logon.
 * @author YU Zhipeng
 * @version 1.0
 * @since 1.0
 * Date: 2009-7-29
 */
@SuppressWarnings("unchecked")
public class BusibuildingSecurityInterceptor implements Interceptor, Constants {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3905462775071664388L;

	private static Map<String, String> LOGON_ROLE = null;
	
	static{
		LOGON_ROLE = new HashMap<String, String>();
		LOGON_ROLE.put(SESSION_LOGON_ADMIN_USER, "admin_logon_input");
//		TODO: Amend the normal user logon result
		LOGON_ROLE.put(SESSION_LOGON_USER, "");
	}
	@Override
	public void destroy() {
	}

	@Override
	public void init() {
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		String result = "";
		ActionContext ctx = invocation.getInvocationContext();
		Object actionSelf = invocation.getAction();
		
		Map<String, Object> session = ctx.getSession();
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
//		TODO: need to add normal user logon action
		if (actionSelf instanceof ProcessAdminLogonAction){
			CookieUtil.setCookie(request, response, COOKIE_NAME, SESSION_LOGON_ADMIN_USER);
			return invocation.invoke();
		}else{
			Object adminUser = session.get(SESSION_LOGON_ADMIN_USER);
			if (adminUser != null){
				invocation.addPreResultListener(new PreResultListener(){
					public void beforeResult(ActionInvocation invocation, String result) {
						CommonActionImpl commonAction = (CommonActionImpl)invocation.getAction();
						if (!commonAction.hasActionErrors() && commonAction.hasFieldErrors() && !commonAction.hasActionMessages()){
							commonAction.addActionError(commonAction.getText("general.message.rectifyInRed"));
						}
					}				
				});
				result = invocation.invoke();
			}else{
				Cookie cookie = CookieUtil.getCookie(request, COOKIE_NAME);
				String key = null;
				if (cookie != null){
					key = cookie.getValue();
				}
				result = LOGON_ROLE.get(key);
				result = result == null ? LOGON_ROLE.get(SESSION_LOGON_ADMIN_USER) : result;
				CommonActionImpl action = (CommonActionImpl)actionSelf;
				action.addActionError(action.getText("general.message.sessionTimeOut"));
				ctx.getSession().clear();
			}
		}
		return result;
	}

}
